// pages/seller-reply/seller-reply.js

var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var commentType = 1;//1:追评 2:回评  默认 追评
Page({
  data: {
    util: util,
    tc: 0,
    uid:0,  //当前登录用户Id
    token:'', //post请求时，当前登录用户token
    orderid : '',
    frm:'',
    title: '',
    isIphone_x: false,
    nav_bgColor: '#ffffff',
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    commentType = options.type ? options.type : 1;
    var that = this;
    

     wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.getLoginUserInfo(function(res){
      //更新数据
      that.setData({
        uid:res.uid,
        token: res.token
      })
    }); 
    var title = options.title ? options.title : "追评";
    this.setData({    
      orderid: options.orderid,
      frm:'',
      title: title,
      tc: app.get_tc()
    });
    wx.setNavigationBarTitle({
      title: title,
    })
  },

subForm : function(e){
      var that = this;    
      var nextUrl = '/pages/'+that.data.frm+'/'+that.data.frm+'?uid=' + that.data.uid;
      var comment = e.detail.value.comment;
      var privatekey = utilMd5.hexMD5(that.data.uid+'commentgoods'+that.data.token);
      var params = {
        publickey: util.publiccode,
        ac: "commentgoods",
        uid: that.data.uid,
        fendanid: that.data.orderid,
        comment: comment,
        privatekey: privatekey,
        commenttype: commentType,
      };
      console.log("提交评论参数：", params);
    util.api({
        data:params,
        method:'POST',
        success:function(responseData){
            var data = responseData.data;
            console.log("提交商品评论：", data);
            if(data.res === true) {
              app.dialogToast('提交成功', 'success', function(){
                  //wx.navigateTo({url: nextUrl})
                  // wx.redirectTo({ url: nextUrl })
                  wx.navigateBack({delta: 1});
                  });
            } else {
                app.dialog('提交失败', 'loading');
            }
        }
    }); 
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      path = path.slice(6);
    }
    if (path) {
      wx.reLaunch({
        url: path,
      })
    }
  },

  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})